package com.todo.biz.infrastructure.repository.impl;


import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.biz.infrastructure.entity.condition.CollectSetCondition;
import com.todo.biz.infrastructure.entity.po.CollectSetPO;
import com.todo.biz.infrastructure.mapper.CollectSetMapper;
import com.todo.biz.infrastructure.repository.CollectSetRepository;
import com.todo.common.utils.MathUtils;
import com.todo.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * (CollectSet)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:03
 */
@Slf4j
@Repository("collectSetRepository")
public class CollectSetRepositoryImpl implements CollectSetRepository {

    @Resource
    private CollectSetMapper collectSetMapper;


    @Override
    public PageResult<CollectSetPO> queryCollectSetByCondition(CollectSetCondition collectSetCondition) {
        PageResult<CollectSetPO> pageResult = new PageResult<>();
        if (collectSetCondition == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        pageResult.setPageNum(collectSetCondition.getPageIndex());
        pageResult.setPageSize(collectSetCondition.getPageSize());
        long count = queryCountByCondition(collectSetCondition);
        if (count <= 0) {
            return pageResult;
        }
        pageResult.setTotal(count);
        pageResult.setPageCount((int) MathUtils.divide(count, collectSetCondition.getPageSize()));
        List<CollectSetPO> collectSetPOList = collectSetMapper.queryCollectSetByCondition(collectSetCondition);
        if (CollectionUtils.isEmpty(collectSetPOList)) {
            pageResult.setHasNext(false);
            return pageResult;
        }
        pageResult.setResults(collectSetPOList);
        pageResult.setCurrentIndex(collectSetPOList.get(0).getId());
        pageResult.setHasNext(collectSetPOList.size() >= collectSetCondition.getPageSize());
        return pageResult;
    }

    @Override
    public Long queryCountByCondition(CollectSetCondition collectSetCondition) {
        if (collectSetCondition == null || collectSetCondition.getPageSize() <= 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return collectSetMapper.queryCountByCondition(collectSetCondition);
    }

    @Override
    public CollectSetPO queryCollectSetPOById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return collectSetMapper.queryCollectSetPOById(id);
    }

    @Override
    public Boolean insertCollectSetPO(CollectSetPO collectSetPO) {
        if (collectSetPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_IS_NULL);
        }
        if (!baseFieldCheck(collectSetPO)) {
            return false;
        }
        return collectSetMapper.insertCollectSetPO(collectSetPO) > 0;
    }

    @Override
    public Boolean updateCollectSetPOById(CollectSetPO collectSetPO) {
        if (collectSetPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_IS_NULL);
        }
        if (!baseFieldCheck(collectSetPO)) {
            return false;
        }
        return collectSetMapper.updateCollectSetPOById(collectSetPO) > 0;
    }


    @Override
    public Boolean removeCollectSetPOById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return collectSetMapper.removeCollectSetPOById(id) > 0;
    }

    @Override
    public CollectSetPO queryCollectSetByTitle(String title) {
        if (StringUtils.isBlank(title)){
            return null;
        }
        return collectSetMapper.queryCollectSetByTitle(title);
    }

    @Override
    public List<CollectSetPO> queryCollectSetByCity(String city, Integer type) {
        if (StringUtils.isBlank(city)){
            return Lists.newArrayList();
        }
        return collectSetMapper.queryCollectSetByCity(city,type);
    }

    @Override
    public List<CollectSetPO> queryCollectSetByTitleAndType(String title, Integer type) {
        if (StringUtils.isBlank(title)){
            return Lists.newArrayList();
        }
        return collectSetMapper.queryCollectSetByTitleAndType(title,type);
    }

    @Override
    public CollectSetPO queryCollectSetByCode(String code) {
        if (StringUtils.isBlank(code)){
            return null;
        }
        return collectSetMapper.queryCollectSetByCode(code);
    }

    @Override
    public Boolean enableCollectSetById(Long id, int status) {
        if (id == null || id < 0){
            return false;
        }
        return collectSetMapper.enableCollectSetById(id,status) > 0;
    }

    private boolean baseFieldCheck(CollectSetPO collectSetPO) {
        if (collectSetPO == null) {
            return false;
        }
        if (collectSetPO.getTitle() == null || collectSetPO.getDesc() == null) {
            return false;
        }
        if (collectSetPO.getCollectType() == null || collectSetPO.getCollectRule() == null) {
            return false;
        }
        return true;
    }
}
